﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using YXShop.IDAL.Product;

namespace YXShop.SQLServerDAL.Product
{
    public class GroupBuy_OnlineSignUpInfo : IGroupBuy_OnlineSignUpInfo
    {
        #region "DataBase Operation"
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <remarks></remarks>
        public int Add(YXShop.Model.Product.GroupBuy_OnlineSignUpInfo model)
        {
            SqlParameter[] paras = (SqlParameter[])this.ValueParas(model);
            string sequel = "Insert into [yxs_groupbuy_onlinesignupinfo](";
            sequel = sequel + "[sid], [uid], [name], [qq], [deposit], [createtime], [count], [state], [signupcount], [sex], [tel], [address], [content])";
            sequel = sequel + "Values(";
            sequel = sequel + "@sid, @uid,@name,@qq,@deposit,@createtime,@count,@state,@signupcount,@sex,@tel,@address,@content) Select scope_IDENTITY() ";
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <remarks></remarks>
        public void Delete(int id)
        {
            string sequel = "Delete From [yxs_groupbuy_onlinesignupinfo]" + this.UpdateWhereSequel;
            SqlParameter[] parameters = (SqlParameter[])this.ValueIDPara(id);
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, parameters);
        }

        /// <summary>
        /// 将一个持久化对象的修改保存到数据库, 并提供事务支持
        /// </summary>
        /// <remarks></remarks>
        public int Amend(YXShop.Model.Product.GroupBuy_OnlineSignUpInfo model)
        {
            string sequel = "Update [yxs_groupbuy_onlinesignupinfo] set  ";
            sequel = sequel + "[sid] =@sid ,[uid] =@uid ,[name]=@name ,[qq] =@qq ,[deposit] =@deposit ,[createtime] =@createtime ,[count] =@count,[state] =@state,[signupcount] =@signupcount,[sex] =@sex,[tel] =@tel,[address] =@address,[content] =@content";
            sequel = sequel + UpdateWhereSequel;
            object obj = ChangeHope.DataBase.SQLServerHelper.ExecuteSql(sequel, (SqlParameter[])this.ValueParas(model));
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改任一字段的记录
        /// </summary>
        /// <param name="uID"></param>
        /// <param name="columnName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Amend(int id, string columnName, Object value)
        {
            string sequel = "Update [yxs_groupbuy_onlinesignupinfo] set ";
            sequel = sequel + "[" + columnName + "] =@Value ";
            sequel = sequel + UpdateWhereSequel;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@Value", value), new SqlParameter("@id", id) };
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(sequel, paras);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 返回数据
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public YXShop.Model.Product.GroupBuy_OnlineSignUpInfo GetModel(System.Data.DataRow row)
        {
            YXShop.Model.Product.GroupBuy_OnlineSignUpInfo model = new YXShop.Model.Product.GroupBuy_OnlineSignUpInfo();
            if (row != null)
            {
                model.ID = int.Parse(row["id"].ToString());
                model.SID = int.Parse(row["sid"].ToString());
                model.UID = int.Parse(row["uid"].ToString());
                model.Name = row["name"].ToString();
                model.QQ = row["qq"].ToString();
                model.Deposit =decimal.Parse(row["deposit"].ToString());
                model.CreateTime = DateTime.Parse(row["createtime"].ToString());
                model.Count = row["count"].ToString();
                model.State = int.Parse(row["state"].ToString());
                model.SignUpCount =int.Parse(row["signupcount"].ToString());
                model.Sex =int.Parse(row["sex"].ToString());
                model.Tel = row["tel"].ToString();
                model.Address = row["address"].ToString();
                model.Content = row["content"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// ID查询
        /// </summary>
        /// <remarks></remarks>
        public YXShop.Model.Product.GroupBuy_OnlineSignUpInfo GetModelID(int id)
        {
            string sequel = SelectSequel + UpdateWhereSequel;
            SqlParameter[] paras = (SqlParameter[])this.ValueIDPara(id);
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(sequel, paras);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return GetModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        public System.Data.DataTable GetActivityList(string sid)
        {
            string strSql = this.SelectSequel + "Where [sid] = @sid";
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@sid", SqlDbType.VarChar, 100);
            paras[0].Value = sid;
            return ChangeHope.DataBase.SQLServerHelper.Query(strSql, paras).Tables[0];
        }
        /// <summary>
        /// 所有记录
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList()
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_groupbuy_onlinesignupinfo [where] 1=1 [order by] id asc";
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        /// <summary>
        /// 前台标签所有记录
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_groupbuy_onlinesignupinfo [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }
        #endregion

        #region "Other function"
        string selectSequel = string.Empty;
        protected string SelectSequel
        {
            get
            {
                if (selectSequel == string.Empty)
                    selectSequel = "Select *,1 PersistStatus  From [yxs_groupbuy_onlinesignupinfo] ";
                return selectSequel;
            }
            set
            {
                this.selectSequel = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected string UpdateWhereSequel
        {
            get
            {
                return " Where [id] = @id";
            }
        }
        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>

        public IDbDataParameter[] ValueParas(YXShop.Model.Product.GroupBuy_OnlineSignUpInfo model)
        {
            SqlParameter[] paras = new SqlParameter[14];
            paras[0] = new SqlParameter("@id", SqlDbType.Int, 4);
            paras[0].Value = model.ID;
            paras[1] = new SqlParameter("@sid", SqlDbType.Int, 4);
            paras[1].Value = model.SID;
            paras[2] = new SqlParameter("@uid", SqlDbType.Int, 4);
            paras[2].Value = model.UID;

            paras[3] = new SqlParameter("@name", SqlDbType.VarChar, 100);
            paras[3].Value = model.Name;
            paras[4] = new SqlParameter("@qq", SqlDbType.VarChar, 50);
            paras[4].Value = model.QQ;

            paras[5] = new SqlParameter("@deposit", SqlDbType.Float, 8);
            paras[5].Value = model.Deposit;
            paras[6] = new SqlParameter("@createtime", SqlDbType.DateTime, 8);
            paras[6].Value = model.CreateTime;
            paras[7] = new SqlParameter("@count", SqlDbType.VarChar, 100);
            paras[7].Value = model.Count;

            paras[8] = new SqlParameter("@state", SqlDbType.TinyInt, 1);
            paras[8].Value = model.State;
            paras[9] = new SqlParameter("@signupcount", SqlDbType.Int, 4);
            paras[9].Value = model.SignUpCount;
            paras[10] = new SqlParameter("@sex", SqlDbType.TinyInt, 1);
            paras[10].Value = model.Sex;

            paras[11] = new SqlParameter("@tel", SqlDbType.VarChar, 20);
            paras[11].Value = model.Tel;
            paras[12] = new SqlParameter("@address", SqlDbType.VarChar, 500);
            paras[12].Value = model.Address;

            paras[13] = new SqlParameter("@content", SqlDbType.VarChar, 200);
            paras[13].Value = model.Content;
            return paras;
        }

        public IDbDataParameter[] ValueIDPara(int id)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@id", id);
            paras[0].DbType = DbType.Int32;
            return paras;
        }

        #endregion


    }
}
